/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dbtest;

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.*;

/**
 *
 * @author Jacob
 */
public class ChessPanel extends JPanel {

//    String imagePath = "images/";
//    JLabel john = new JLabel("John");
    Tile[] tiles;
    private Game game;

    public ChessPanel(Game game) {
        this.game = game;
        this.setLayout(new GridLayout(8, 8));
//        ImagePanel black = new ImagePanel(imagePath + "black64x64.png");
//        ImagePanel white = new ImagePanel(imagePath + "white64x64.png");
//        add(white);
//        add(black);
//        add(white);
        //setSize(800, 600);
        BufferedImage blackTile;
        BufferedImage whiteTile;
        tiles = new Tile[64];
        
        int x = 0;
        int y = 0;
        int i = 0;
        for (int j = 0; j < 8; j++) {
            for (int k = 0; k < 8; k++) {
                tiles[i] = new Tile(k * 64, j * 64);
                add(tiles[i]);
                tiles[i].addMouseListener(new TileListener(tiles[i]));
                //System.out.println("x: " + tiles[i].getX() + " y: " + tiles[i].getY());
                i++;
            }
        }
        tiles[7].setPiece(new Piece(tiles[7].col, tiles[7].row, Piece.Color.WHITE, Piece.Type.PAWN, "w_pawn1"));
        
//        try {
//            blackTile = ImageIO.read(new File(imagePath + "black64x64.png"));
//            whiteTile = ImageIO.read(new File(imagePath + "white64x64.png"));
//            JLabel tiles[] = new JLabel[64];
//            for (int i = 0; i < 64; i++) {
//                int row = (i / 8) % 2;
//                if (row == 0) {
////                    square.setBackground(i % 2 == 0 ? Color.blue : Color.white);
//                    switch (i % 2) {
//                        case 0:
//                            tiles[i] = new JLabel(new ImageIcon(blackTile));
//                            break;
//                        case 1:
//                            tiles[i] = new JLabel(new ImageIcon(whiteTile));
//                            break;
//                    }
//                } else {
////                    square.setBackground(i % 2 == 0 ? Color.white : Color.blue);
//                    switch (i % 2) {
//                        case 0:
//                            tiles[i] = new JLabel(new ImageIcon(whiteTile));
//                            break;
//                        case 1:
//                            tiles[i] = new JLabel(new ImageIcon(blackTile));
//                            break;
//                    }
//                }
//
//
//                add(tiles[i]);
//            }
//        } catch (IOException ex) {
//            Logger.getLogger(ChessPanel.class.getName()).log(Level.SEVERE, null, ex);
//        }
//        JLabel picLabel = new JLabel(new ImageIcon(myPicture));
//        add(picLabel);
//
//        setLayout(
//                new GridLayout(8, 8));
        setVisible(true);
        repaint();
        
    }
    
    @Override
    public void paintComponent(Graphics g) {
        //System.out.println("paint!");
        super.paintComponent(g);
        for (int i = 0; i < tiles.length; i++) {
            tiles[i].paint(g);
        }
    }
    
    private class TileListener implements MouseListener {

        Tile tile;
        
        public TileListener(Tile tile) {
            this.tile = tile;
        }
        @Override
        public void mouseClicked(MouseEvent e) {
            int player = game.getActivePlayer();
            Piece sp = game.getSelectedPiece();
            if (sp == null) {
                if (tile.getPiece() != null) {
                    if (player == Game.BLACK && sp.getColor() == Piece.Color.WHITE
                            || player == Game.WHITE && sp.getColor() == Piece.Color.BLACK) {
                        return;
                    }
                    game.setSelectedPiece(tile.getPiece());
                    tile.setPiece(null);
                }
            } else if (sp.canMove(tile)) {
                sp.move(tile);
                game.submitMove(sp);
                game.setSelectedPiece(null);
                repaint();
            }
        }

        @Override
        public void mousePressed(MouseEvent e) {
            
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            
        }

        @Override
        public void mouseEntered(MouseEvent e) {
            
        }

        @Override
        public void mouseExited(MouseEvent e) {
            
        }
        
    }
}
